﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

using mshtml;

namespace Xin478.CSWork.Client
{
    public class MyRichEditBox : Control
    {
        System.Windows.Forms.WebBrowser webBrowser1;

        public MyRichEditBox()
        {
            webBrowser1 = new System.Windows.Forms.WebBrowser();
            webBrowser1.Dock = DockStyle.Fill;
            webBrowser1.Size = new System.Drawing.Size(348, 255);
            webBrowser1.PreviewKeyDown += new PreviewKeyDownEventHandler(webBrowser1_PreviewKeyDown);
            webBrowser1.IsWebBrowserContextMenuEnabled = false;
            webBrowser1.AllowNavigation = false;
            webBrowser1.WebBrowserShortcutsEnabled = false;
            webBrowser1.Navigate("about:blank");
            Controls.Add(webBrowser1);
            ((IHTMLDocument2)this.webBrowser1.Document.DomDocument).designMode = "on";
        }
        

        void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if(e.Control)
            {
                switch (e.KeyCode)
                {
                    case Keys.V:
                        object o = Clipboard.GetData(DataFormats.Bitmap);
                        Image img = o as Image;
                        if (img != null && TempAbsolutePath!=null)
                        {
                            string path = TempAbsolutePath + img.GetHashCode().ToString() + ".png";
                            img.Save(path, System.Drawing.Imaging.ImageFormat.Png);
                            webBrowser1.Document.ExecCommand("InsertImage", false, "file:///" + path);
                        }
                        else
                        {
                            webBrowser1.Document.ExecCommand("Paste", false, null);
                        }
                        break;
                    case Keys.A:
                        webBrowser1.Document.ExecCommand("SelectAll", false, null);
                        break;

                    case Keys.C:
                        webBrowser1.Document.ExecCommand("Copy", false, null);
                        break;

                    case Keys.X:
                        webBrowser1.Document.ExecCommand("Cut", false, null);
                        break;

                    case Keys.Z:
                        webBrowser1.Document.ExecCommand("Undo", false, null);
                        break;

                    case Keys.Y:
                        webBrowser1.Document.ExecCommand("Redo", false, null);
                        break;
                }
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string HtmlText
        {
            get { return this.webBrowser1.Document.Body.InnerHtml; }
            set { if(this.webBrowser1.Document.Body!=null) this.webBrowser1.Document.Body.InnerHtml = value; }
        }
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string Url
        {
            get { return this.webBrowser1.Url.ToString(); }
            set
            {
                this.webBrowser1.Url = new Uri(value);
            }
        }
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string TempAbsolutePath { get; set; }
        public bool EnableScroll
        {
            get { return this.webBrowser1.ScrollBarsEnabled; }
            set { this.webBrowser1.ScrollBarsEnabled = value; }
        }

        #region IDisposable 成员
        ~MyRichEditBox()
        {
            ((IHTMLDocument2)this.webBrowser1.Document.DomDocument).close();
        }

        #endregion
    }

    /*
    public class MyRichTextBox1 :RichTextBox
    {
        public class MyRtf
        {
            private string rtfText;
            private Hashtable images = new Hashtable();
            private MyRtf() {}

            public static MyRtf ConvertToMyRtf(string rtf)
            {
                int index=0;
                MyRtf myRtf = new MyRtf();
                if (string.IsNullOrEmpty(rtf) || (index = rtf.IndexOf("{\\pict")) == -1)
                {
                    myRtf.rtfText = rtf;
                    return myRtf;
                }

                StringBuilder sb = new StringBuilder();
                int t, t1;
                sb.Append(rtf, 0, index);
                do
                {
                    t = rtf.IndexOf('}', index + 1);
                    if (t == -1)
                    {
                        myRtf.rtfText = rtf;
                        return myRtf;
                    }

                    t1 = rtf.IndexOf("\r\n", index + 1);
                    if (t1 == -1)
                    {
                        myRtf.rtfText = rtf;
                        return myRtf;
                    }

                    string imgstr=rtf.Substring(t1,t-t1).Replace("\r\n","");


                    int count = imgstr.Length / 2;
                    byte[] imgb=new byte[count];
                    for(int i=0;i<count;++i)
                    {
                        imgb[i]=Convert.ToByte( imgstr.Substring(i*2,2),16);
                    }
                    string crc = string.Format("{0:X8}",CRC32.ByteCRC(imgb));

                    if(!myRtf.images.ContainsKey(crc)) myRtf.images.Add(crc,imgb);
                    sb.Append("{IMG|").Append(crc).Append("|").Append(rtf.Substring(index + 6, t1 - index - 6)).Append("}");

                    t1 = rtf.IndexOf("{\\pict",t1);
                    if (t1 == -1)
                    {
                        sb.Append(rtf.Substring(t, rtf.Length - t));
                        break;
                    }
                    else
                    {
                        sb.Append(rtf.Substring(t, t1-t));
                    }

                } 
                while (true);

                myRtf.rtfText = sb.ToString();
                return myRtf;
            }
        }

        public MyRtf MyFormatedRtf
        {
            get
            {
                return MyRtf.ConvertToMyRtf(this.Rtf);
            }
        }
    }
     * */
}
